this.BX=this.BX||{};this.BX.Landing=this.BX.Landing||{};this.BX.Landing.UI=this.BX.Landing.UI||{};(function(e,t,n,o,i,r,s,a,p,u,d){"use strict";function l(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);t&&(o=o.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,o)}return n}function c(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?l(Object(n),!0).forEach((function(t){babelHelpers.defineProperty(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):l(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}var h=function(e){babelHelpers.inherits(t,e);function t(){var e;var o=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};babelHelpers.classCallCheck(this,t);e=babelHelpers.possibleConstructorReturn(this,babelHelpers.getPrototypeOf(t).call(this,o));r.Dom.addClass(e.layout,"landing-ui-form-cards");e.type="cards";e.code=o.code;e.id="".concat(e.code.replace(".",""),"-").concat(r.Text.getRandom());e.presets=o.presets;e.childForms=new n.FormCollection;e.presetForm=new n.FormCollection;e.sync=o.sync;e.forms=o.forms;e.wheelEventName=window.onwheel?"wheel":"mousewheel";e.onFormRemove=e.onFormRemove.bind(babelHelpers.assertThisInitialized(e));e.onAddCardClick=e.onAddCardClick.bind(babelHelpers.assertThisInitialized(e));e.onMouseWheel=e.onMouseWheel.bind(babelHelpers.assertThisInitialized(e));e.onDragEnd=e.onDragEnd.bind(babelHelpers.assertThisInitialized(e));e.addButton=e.createAddButton();e.draggable=new a.Draggable({container:e.body,context:parent.window,draggable:".landing-ui-form-cards-item",dragElement:".landing-ui-form-card-item-header-drag",type:a.Draggable.MOVE,offset:{y:-65}});e.draggable.subscribe("end",e.onDragEnd);setTimeout((function(){e.value=e.serialize()}));e.adjustLastFormState();r.Dom.append(e.addButton.layout,e.footer);return e}babelHelpers.createClass(t,[{key:"createAddButton",value:function e(){return new BX.Landing.UI.Button.BaseButton("add-card-".concat(r.Text.getRandom()),{className:"landing-ui-card-add-button",text:o.Loc.getMessage("LANDING_CARDS_FORM_ADD_BUTTON"),onClick:this.onAddCardClick})}},{key:"onFormRemove",value:function e(t){this.childForms.remove(t.getTarget());this.sortForms();this.adjustLastFormState()}},{key:"onDragEnd",value:function e(){var t=this;setTimeout((function(){t.sortForms()}))}},{key:"sortForms",value:function e(){var t=babelHelpers.toConsumableArray(this.body.children);this.childForms.sort((function(e,n){var o=parseInt(t.indexOf(e.wrapper));var i=parseInt(t.indexOf(n.wrapper));return o<i?-1:1}));this.childForms.forEach((function(e,t){var n=e.selector.split("@"),o=babelHelpers.slicedToArray(n,1),i=o[0];e.selector="".concat(i,"@").concat(t)}))}},{key:"addChildForm",value:function e(t){this.childForms.add(t);t.subscribe("onRemove",this.onFormRemove);r.Dom.append(t.wrapper,this.body);this.adjustLastFormState()}},{key:"addPresetForm",value:function e(t){this.presetForm.add(t);t.wrapper.hidden=true;r.Dom.append(t.wrapper,this.body);this.adjustLastFormState()}},{key:"onAddCardClick",value:function e(){if(r.Type.isPlainObject(this.presets)&&Object.keys(this.presets).length>0){this.showPresetsPopup()}else{this.addEmptyCard()}}},{key:"onPresetItemClick",value:function e(t){var n=this.presets[t];var o=this.presetForm.find((function(e){return e.preset.id===t})).clone();o.selector="".concat(o.selector.split("@")[0],"@").concat(this.childForms.length);o.oldIndex=this.childForms.length;o.preset=r.Runtime.clone(n);o.preset.id=t;this.addChildForm(o);this.adjustLastFormState();this.popup.close();if(r.Type.isPlainObject(n.values)){o.fields.forEach((function(e){var t=e.selector.split("@")[0];if(t in n.values){e.setValue(n.values[t]);if(e instanceof d.TextField){BX.fireEvent(e.input,"input")}}if(r.Type.isArray(n.disallow)){var o=!!n.disallow.find((function(e){return t===e}));if(o){e.layout.hidden=true}}}))}}},{key:"showPresetsPopup",value:function e(){var n=this;if(this.popup){t.popups.map((function(e){e.popupWindow.close();e.popupWindow.destroy()}))}this.popup=new BX.PopupMenuWindow({id:"cards_list_"+r.Text.getRandom(),bindElement:this.addButton.layout,items:Object.keys(this.presets).map((function(e){return{html:n.presets[e].name,className:"landing-ui-form-cards-preset-popup-item menu-popup-no-icon",onclick:n.onPresetItemClick.bind(n,e)}})),autoHide:true,maxHeight:176,minHeight:87});t.popups.push(this.popup);r.Event.bind(this.popup.popupWindow.popupContainer,"mouseover",this.onMouseOver.bind(this));r.Event.bind(this.popup.popupWindow.popupContainer,"mouseleave",this.onMouseLeave.bind(this));var o=p.PageObject.getRootWindow();r.Event.bind(o.document,"click",this.onDocumentClick.bind(this));r.Dom.append(this.popup.popupWindow.popupContainer,this.addButton.layout.closest(".landing-ui-panel-content-body-content"));this.popup.popupWindow.show();this.adjustPopupPosition()}},{key:"onMouseOver",value:function e(){var t=this.popup.popupWindow.getPopupContainer();r.Event.bind(t,this.wheelEventName,this.onMouseWheel,true);r.Event.bind(t,"touchmove",this.onMouseWheel,true)}},{key:"onMouseLeave",value:function e(){var t=this.popup.popupWindow.getPopupContainer();r.Event.unbind(t,this.wheelEventName,this.onMouseWheel,true);r.Event.unbind(t,"touchmove",this.onMouseWheel,true)}},{key:"onMouseWheel",value:function e(t){var n=this;t.stopPropagation();t.preventDefault();var o=i.Content.getDeltaFromEvent(t);var r=this.popup.popupWindow.getContentContainer(),s=r.scrollTop;requestAnimationFrame((function(){n.popup.popupWindow.contentContainer.scrollTop=s-o.y}))}},{key:"onDocumentClick",value:function e(t){if(this.popup.popupWindow&&!r.Dom.hasClass(t.target,"landing-ui-button-text")&&!r.Dom.hasClass(t.target,"landing-ui-card-add-button")){this.popup.popupWindow.close()}}},{key:"adjustPopupPosition",value:function e(){var t=this;if(this.popup.popupWindow){requestAnimationFrame((function(){var e=t.addButton.layout.closest(".landing-ui-panel-content-body-content");var n=BX.Landing.Utils.offsetTop(t.addButton.layout,e);var o=BX.Landing.Utils.offsetLeft(t.addButton.layout,e);var i=t.addButton.layout.getBoundingClientRect();var r=t.popup.popupWindow.popupContainer.getBoundingClientRect();var s=14;t.popup.popupWindow.popupContainer.style.top="".concat(n+i.height+s,"px");t.popup.popupWindow.popupContainer.style.left="".concat(o-r.width/2+i.width/2,"px");t.popup.popupWindow.setAngle({offset:83,position:"top"})}))}}},{key:"addEmptyCard",value:function e(){var t=r.Runtime.clone(this.childForms[0].data);var n="".concat(t.selector.split("@")[0],"@").concat(this.childForms.length);t.selector=n;var o=this.childForms[0].clone(t);o.oldIndex=this.childForms.length;o.selector=n;o.fields.forEach((function(e){return e.reset()}));this.addChildForm(o);this.adjustLastFormState()}},{key:"getVisibleForms",value:function e(){return babelHelpers.toConsumableArray(this.body.children).filter((function(e){return!e.hidden}))}},{key:"adjustLastFormState",value:function e(){var t=this.getVisibleForms();if(t.length===1){r.Dom.addClass(t[0],"landing-ui-disallow-remove");return}babelHelpers.toConsumableArray(t).forEach((function(e){r.Dom.removeClass(e,"landing-ui-disallow-remove")}))}},{key:"serialize",value:function e(){return this.childForms.map((function(e){return e.serialize()}))}},{key:"getIndexesMap",value:function e(){return this.childForms.reduce((function(e,t,n){return c(c({},e),{},babelHelpers.defineProperty({},n,t.oldIndex))}),{})}},{key:"getUsedPresets",value:function e(){return this.childForms.reduce((function(e,t){if(r.Type.isPlainObject(t.preset)){var n=t.selector.split("@"),o=babelHelpers.slicedToArray(n,2),i=o[1];e[i]=t.preset.id}return e}),{})}},{key:"isChanged",value:function e(){return JSON.stringify(this.value)!==JSON.stringify(this.serialize())}}]);return t}(t.BaseForm);babelHelpers.defineProperty(h,"popups",[]);e.CardsForm=h})(this.BX.Landing.UI.Form=this.BX.Landing.UI.Form||{},BX.Landing.UI.Form,BX.Landing.UI.Collection,BX.Landing,BX.Landing.UI.Panel,BX,BX.Landing.UI.Form,BX.UI.DragAndDrop,BX.Landing,BX.Event,BX.Landing.UI.Field);
//# sourceMappingURL=cardsform.bundle.map.js